package com.py.product.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.py.api.PyCodeApi;
import com.py.product.domain.InventoryDetails;
import com.py.product.domain.PySuppliers;
import com.py.product.domain.WhInventory;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.py.product.domain.Product;
import com.py.product.service.IProductService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 货品信息Controller
 * 
 * @author he
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController
{
    @Autowired
    private IProductService productService;

    @Resource
    private PyCodeApi pyCodeApi;

    /**
     * 获取编码
     */
    @PostMapping("/getcode")
    public Map<String, Object> getCode(@RequestParam(value = "code",required = false) Integer code){
        return pyCodeApi.generateCode(code);
    }



    /**
     * 查询详情
     */
    @GetMapping("/selectProductDetails")
    public Product selectProductDetails(@RequestParam("productId") Long productId){
        return productService.selectProductDetails(productId);
    }


    /**
     * 查询供应商下拉列表
     */
    @GetMapping("/selectPySuppliers")
    public List<PySuppliers> selectPySuppliers(){
        return productService.selectPySuppliers();
    }


    /**
     * 查询货品信息列表
     */
    @RequiresPermissions("product:product:list")
    @GetMapping("/list")
    public TableDataInfo list(Product product)
    {
        startPage();
        List<Product> list = productService.selectProductList(product);
        System.out.println("list:"+list);
        return getDataTable(list);
    }
    /**
     * 导出货品信息列表
     */
    @RequiresPermissions("product:product:export")
    @Log(title = "货品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Product product)
    {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "货品信息数据");
    }


    /**
     * 获取货品信息详细信息
     */
    @RequiresPermissions("product:product:query")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(productService.selectProductByProductId(productId));
    }

//    @RequiresPermissions("product:product:query")
//    @GetMapping(value = "/warehouse/{warehouseName}")
//    public List<Product> getInfo(@PathVariable("warehouseName") int warehouseId)
//    {
//        System.out.println("akdhaosdjfhgja:"+warehouseId);
//        List<Product> products = productService.addProductByname(warehouseId);
//        System.out.println(products);
//        return products;
//    }
    @RequiresPermissions("product:product:query1")
    @GetMapping(value = "/warehouse/{warehouseName1}")
    public List<Product> getInfo1(@PathVariable("warehouseName1") int warehouseId)
    {
        System.out.println("akdhaosdjfhgja:"+warehouseId);
        List<Product> products = productService.addProductByname(warehouseId);
        System.out.println(products);
        return products;
    }

    /**
     * 新增货品信息
     */
    @RequiresPermissions("product:product:add")
    @Log(title = "货品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Product product)
    {
        System.out.println("number:"+product.getNumber());
        System.out.println("codeId:"+product.getCodeId());
        pyCodeApi.saveCode(product.getCodeId(),product.getNumber());
        product.setCreateBy(SecurityUtils.getUsername());
        product.setCreateTime(new Date());
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改货品信息
     */
    @RequiresPermissions("product:product:edit")
    @Log(title = "货品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Product product)
    {
        product.setUpdateBy(SecurityUtils.getUsername());
        product.setUpdateTime(new Date());
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 修改库存信息
     */
//    @RequiresPermissions("product:product:edit")
//    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
//    @PutMapping(value = "/xgai")
//    public AjaxResult edit(@RequestBody Map<String,Object> whInventory)
//    {
//        System.out.println("数据："+whInventory);
//       // System.out.println("id:"+whInventory.getInventoryQueryId());
//        /*System.out.println("id:"+whInventory.getInventoryQueryId());
//        System.out.println("当前库存:"+whInventory.getCurrentInventory());
//        whInventory.setCurrentInventory(whInventory.getCurrentInventory());
//        whInventory.setInventoryQueryId(whInventory.getInventoryQueryId());
//        whInventory.setUpdateBy(SecurityUtils.getUsername());
//        return toAjax(productService.xgai(whInventory));*/
//        return null;
//    }



    /**
     * 删除货品信息
     */
    @RequiresPermissions("product:product:remove")
    @Log(title = "货品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(productService.deleteProductByProductIds(productIds));
    }

    /**
     * 查询所有货品信息
     */
    @RequestMapping("/selectByProduct")
    public TableDataInfo selectByProduct(Product product){
        System.out.println("进来近似数:"+product);
        startPage();
        List<Product> list =productService.selectAllByProduct(product);
        list.forEach(System.out::println);
        return getDataTable(list);
    }
}
